//
//  FireView.m
//  test
//
//  Created by dlios on 15-7-9.
//  Copyright (c) 2015年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "FireView.h"


@implementation FireView

@synthesize fireEmitter;
@synthesize smokeEmitter;

- (void)dealloc {
    [self.fireEmitter removeFromSuperlayer];
    self.fireEmitter = nil;
    [self.smokeEmitter removeFromSuperlayer];
    self.smokeEmitter = nil;
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        CGRect viewBounds = self.layer.bounds;
        
        // Create the emitter layers
        self.fireEmitter	= [CAEmitterLayer layer];
        self.smokeEmitter	= [CAEmitterLayer layer];
        
        // Place layers just above the tab bar
        self.fireEmitter.emitterPosition = CGPointMake(viewBounds.size.width/2.0, viewBounds.size.height);
//        self.fireEmitter.emitterSize	= CGSizeMake(viewBounds.size.width/10.0, viewBounds.size.height / 10.0);
        self.fireEmitter.emitterMode	= kCAEmitterLayerOutline;
        self.fireEmitter.emitterShape	= kCAEmitterLayerLine;
        // with additive rendering the dense cell distribution will create "hot" areas
        self.fireEmitter.renderMode		= kCAEmitterLayerAdditive;
        
        self.smokeEmitter.emitterPosition = CGPointMake(viewBounds.size.width/2.0, viewBounds.size.height);
        self.smokeEmitter.emitterMode	= kCAEmitterLayerPoints;
        
        // Create the fire emitter cell
        CAEmitterCell* fire = [CAEmitterCell emitterCell];
        [fire setName:@"fire"];
        

        fire.emissionLongitude  = M_PI ;
        fire.velocity			= -80;
        fire.velocityRange		= 30;
        fire.emissionRange		= 1.1;
        fire.yAcceleration		= -200;
        fire.scaleSpeed			= 0.3;
        
        fire.color = [[UIColor colorWithRed:0.758 green:0.325 blue:0.153 alpha:0.100] CGColor];
        fire.contents = (id) [[UIImage imageNamed:@"DazFire1"] CGImage];
        
        
        // Create the smoke emitter cell
        CAEmitterCell* smoke = [CAEmitterCell emitterCell];
        [smoke setName:@"smoke"];
        
        smoke.birthRate			= 11;
        smoke.emissionLongitude = -M_PI / 2;
        smoke.lifetime			= 10;
        smoke.velocity			= -40;
        smoke.velocityRange		= 20;
        smoke.emissionRange		= M_PI / 4;
        smoke.spin				= 1;
        smoke.spinRange			= 6;
        smoke.yAcceleration		= -160;
        smoke.contents			= (id) [[UIImage imageNamed:@"DazSmoke"] CGImage];
        smoke.scale				= 0.1;
        smoke.alphaSpeed		= -0.12;
        smoke.scaleSpeed		= 0.7;
        
        
        // Add the smoke emitter cell to the smoke emitter layer
        self.smokeEmitter.emitterCells	= [NSArray arrayWithObject:smoke];
        self.fireEmitter.emitterCells	= [NSArray arrayWithObject:fire];
        [self.layer addSublayer:self.smokeEmitter];
        [self.layer addSublayer:self.fireEmitter];
        
        
        [self.fireEmitter setValue:[NSNumber numberWithInt:200]
                        forKeyPath:@"emitterCells.fire.birthRate"];
        [self.fireEmitter setValue:[NSNumber numberWithFloat:0.2]
                        forKeyPath:@"emitterCells.fire.lifetime"];
        [self.fireEmitter setValue:[NSNumber numberWithFloat:0.08]
                        forKeyPath:@"emitterCells.fire.lifetimeRange"];
        self.fireEmitter.emitterSize = CGSizeMake(8, 0);
        [self.smokeEmitter setValue:[NSNumber numberWithInt:0.6]
                         forKeyPath:@"emitterCells.smoke.lifetime"];
        [self.smokeEmitter setValue:(id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:0.051] CGColor]
                         forKeyPath:@"emitterCells.smoke.color"];
    }
    return self;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
